using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Engine.Components.Workers
{
    enum JobStatus { NewTask, JobTerminated, NoMoreJob};
    public sealed class SyncEvents
    {
        public SyncEvents()
        {

            _newItemEvent = new AutoResetEvent(false);
            _exitThreadEvent = new ManualResetEvent(false);
            _noMoreJobEvent = new ManualResetEvent(false);
            _eventArray = new WaitHandle[3];
            _eventArray[0] = _newItemEvent;
            _eventArray[1] = _exitThreadEvent;
            _eventArray[2] = _noMoreJobEvent;
        }

        public EventWaitHandle ExitThreadEvent
        {
            get { 
                return _exitThreadEvent; }
        }
        public EventWaitHandle NewItemEvent
        {
            get { 
                return _newItemEvent; }
        }
        public EventWaitHandle NoMoreEvent
        {
            get { 
                return _noMoreJobEvent; }
        }
        public WaitHandle[] EventArray
        {
            get { return _eventArray; }
        }

        private EventWaitHandle _newItemEvent;
        private EventWaitHandle _exitThreadEvent;
        private EventWaitHandle _noMoreJobEvent;
        private WaitHandle[] _eventArray;
    }

    public class Job
    {
        private Job()
        { 
        }
        public Queue<object> Queue = new Queue<object>();
        public SyncEvents SyncEvents = new SyncEvents();

        public static Job CreateJob()
        {
            return new Job();
        }

        public static Job CreateFirstJob()
        {
            Job j = new Job();
            j.SyncEvents.NoMoreEvent.Set();
            j.SyncEvents.NewItemEvent.Set();
            return j;
        }
    }
}
